<?php

namespace app\index\controller;

use think\Controller;
use think\Cookie;
use think\Db;
use think\Request;
use app\index\Controller\Send;
use think\Session;

class Register extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view('register/register', ['title' => 'xx阅读',]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function yzm(Request $request)
    {
        $mobile = $request->param();
        $num = $mobile['num'];
        $send = new Send();
        $res = $send->sendTemplateSMS($num);


    }

    public function RegisterAuthor()
    {
        return view('register/registerauthor', [
            'title' => 'becomeauthor',
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //todo:接收前台的表单
        $table = $request->post();
        $verify = session::get('verify');
        if ($verify == $table['verify']) {
            //todo:验证成功,将账号存入数据库
            $data = '';
            $data['name'] = $table['user'];
            $data['tel'] = $table['tel'];
            $data['password'] = md5($table['pass']);
            $data['rtime'] = time();

            $res = Db::name('user')->insert($data);
            //todo:设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
            $this->success('新增成功', 'index/Index/index');
        } else {

            //todo:错误页面的默认跳转页面是返回前一页，通常不需要设置
            $this->error('新增失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read(Request $request)
    {
        $data = $request->post();
        $ids = Session::get('user');
        $idc = Cookie::get('loginid');


        if($ids == null){
            $data['uid']=$idc['id'];
            $res = db('author_user')->insert($data);
        }else{
            $data['uid']=$ids['id'];
            $res = db('author_user')->insert($data);
        }


        if ($res) {
            $this->success('成为作者成功', 'index/Index/index');
        } else {
            $this->redirect('index/Index/index');
        }
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
